<template>
  <ul class="list">
    <li
      class="item"
      v-for="item in letters"
      :key="item"
      :ref="item"
      @touchstart.prevent="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd"
      @click="handleLetter"
    >
      {{item}}
    </li>
  </ul>
</template>
<script type="text/ecmascript-6">
export default {
  name: 'cityAlphabet',
  props: {
    List: Object
  },
  computed: {
    letters () {
      const letters = []
      for (let i in this.List) {
        letters.push(i)
      }
      return letters
    }
  },
  data () {
    return {
      handlestates: false,
      startY: '',
      timer: null
    }
  },
  created () {
    // this.overscroll(document.querySelector('.item'))
    // document.body.addEventListener('touchmove', function (evt) {
    //   if (!evt._isScroller) {
    //     evt.preventDefault()
    //   }
    // })
  },
  updated () {
    this.startY = this.$refs['A'][0].offsetTop
  },
  methods: {
    // overscroll (el) {
    //   el.addEventListener('addEventListener', function () {
    //     const top = el.scrollTop
    //     const totalScroll = el.scrollHeight
    //     const currentScroll = top + el.offsetHeight
    //     if (top === 0) {
    //       el.scrollTop = 1
    //     } else if (currentScroll === totalScroll) {
    //       el.scrollTop = top - 1
    //     }
    //   })
    //   el.addEventListener('touchmove', function (evt) {
    //     if (el.offsetHeight < el.scrollHeight) {
    //       evt._isScroller = true
    //     }
    //   })
    // },
    handleLetter (e) {
      this.$emit('change', e.target.innerText)
    },
    handleTouchStart () {
      this.handlestates = true
    },
    handleTouchMove (e) {
      if (this.handlestates) {
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          const touchY = e.touches[0].clientY - 79
          const index = Math.floor((touchY - this.startY) / 20)
          if (index >= 0 && index < this.letters.length) {
            this.$emit('change', this.letters[index])
          }
        }, 15)
      }
    },
    handleTouchEnd () {
      this.handlestates = false
    }
  }
}
</script>
<style lang="stylus" rel="stylesheet/stylus" scoped>
  @import "~styles/varibles.styl";
  .list
    display: flex
    flex-direction: column
    justify-content: center
    position: absolute
    right: 0
    top: 1.6rem
    bottom: 0
    width: .4rem
    .item
      color: $bgColor
      line-height: .4rem
      text-align: center
</style>
